package com.ytjj.qmyx.mall.api.h5.v1.subsidies;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.model.dto.RecordSubsidyDataDto;
import com.ytjj.qmyx.mall.model.response.OldForNewExtResponse;
import com.ytjj.qmyx.mall.model.response.OldForNewReceiveResponse;
import com.ytjj.qmyx.mall.service.ActivityService;
import com.ytjj.qmyx.mall.service.CommonService;
import com.ytjj.qmyx.users.client.UsersClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Optional;


/**
 * 活动控制器 (补贴活动)
 *
 * 
 * @version 1.0
 * 
 */
@RestController
@RequestMapping("/v1/subsidies")
@Slf4j
public class SubsidiesController extends BaseController {

    private final ActivityService activityService;

    private final CommonService commonService;

    private final UsersClient usersClient;

    @Resource
    private RedisService redisService;

    public SubsidiesController(ActivityService activityService, CommonService commonService, UsersClient usersClient) {
        this.activityService = activityService;
        this.commonService = commonService;
        this.usersClient = usersClient;
    }

    /**
     * 以旧换新栏目 (h5)
     *
     * @param productId
     * @return
     */
    @GetMapping("/queryOldForNewData")
    public CommonResult queryOldForNewColumn(@RequestParam(value = "productId", required = false) Integer productId) {
        OldForNewExtResponse result = activityService.queryOldForNewColumn(productId);
        String token = TokenUtil.getToken();
        String recordKey = com.ytjj.qmyx.mall.constants.RedisConstants.RECORD_SUBSIDYKEY + token;
        String recordDto = redisService.get(recordKey);
        if (StringUtils.isEmpty(token) || StringUtils.isEmpty(recordDto)) {
            result.setTotalReceive(BigDecimal.valueOf(0));
            result.setUsed(BigDecimal.valueOf(0));
            result.setNotUsed(BigDecimal.valueOf(0));
            result.setNotUsedNum(0);
            return CommonResult.success(result);
        }
        RecordSubsidyDataDto recordSubsidyDataDto = Optional.ofNullable(JSONObject.parseObject(recordDto, RecordSubsidyDataDto.class)).orElse(new RecordSubsidyDataDto());
        BigDecimal totalReceive = Optional.ofNullable(recordSubsidyDataDto.getReceived()).orElse(BigDecimal.ZERO);
        BigDecimal used = Optional.ofNullable(recordSubsidyDataDto.getUsed()).orElse(BigDecimal.ZERO);
        result.setTotalReceive(totalReceive.setScale(0, BigDecimal.ROUND_UP));
        result.setUsed(used.setScale(0, BigDecimal.ROUND_UP));
        result.setNotUsed(totalReceive.compareTo(used) > 0
                ? totalReceive.subtract(used).setScale(0, BigDecimal.ROUND_UP)
                : BigDecimal.ZERO);
        result.setNotUsedNum(redisService.getHashEntries(com.ytjj.qmyx.mall.constants.RedisConstants.SUBSIDYKEY + token).size());
        return CommonResult.success(result);
    }

    /**
     * 获取补贴未领取的商品
     *
     * @return
     */
    @GetMapping("/getNotReceiveList")
    public CommonResult getNotReceiveList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        OldForNewReceiveResponse response = activityService.getNotReceiveList(pageNum,pageSize);
        return CommonResult.success(response);
    }

    /**
     * 获取用户已补贴商品 h5
     *
     * @return
     */
    @GetMapping("/getOldForNewProductsOfH5User")
    public CommonResult getOldForNewProductsOfH5User(@RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag) {
        return CommonResult.success(activityService.getOldForNewProductsOfH5User(flag, null, null));
    }

    /**
     * 品牌补贴,外页
     *
     * @return
     */
    @GetMapping("/getBrandList")
    public CommonResult getBrandList() {
        return CommonResult.success(activityService.getBrandList());
    }

}
